/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package carstoredb.models;

import carstoredb.models.exceptions.NonexistentEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author wuhao
 */
public class OrderInfoJpaController implements Serializable {

	public OrderInfoJpaController(EntityManagerFactory emf) {
		this.emf = emf;
	}
	private EntityManagerFactory emf = null;

	public EntityManager getEntityManager() {
		return emf.createEntityManager();
	}

	public void create(OrderInfo orderInfo) {
		EntityManager em = null;
		try {
			em = getEntityManager();
			em.getTransaction().begin();
			Carstore storeId = orderInfo.getStoreId();
			if (storeId != null) {
				storeId = em.getReference(storeId.getClass(), storeId.getStoreId());
				orderInfo.setStoreId(storeId);
			}
			User userId = orderInfo.getUserId();
			if (userId != null) {
				userId = em.getReference(userId.getClass(), userId.getUserId());
				orderInfo.setUserId(userId);
			}
			Car carID = orderInfo.getCarID();
			if (carID != null) {
				carID = em.getReference(carID.getClass(), carID.getId());
				orderInfo.setCarID(carID);
			}
			em.persist(orderInfo);
			if (storeId != null) {
				storeId.getOrderInfoCollection().add(orderInfo);
				storeId = em.merge(storeId);
			}
			if (userId != null) {
				userId.getOrderInfoCollection().add(orderInfo);
				userId = em.merge(userId);
			}
			if (carID != null) {
				carID.getOrderInfoCollection().add(orderInfo);
				carID = em.merge(carID);
			}
			em.getTransaction().commit();
		} finally {
			if (em != null) {
				em.close();
			}
		}
	}

	public void edit(OrderInfo orderInfo) throws NonexistentEntityException, Exception {
		EntityManager em = null;
		try {
			em = getEntityManager();
			em.getTransaction().begin();
			OrderInfo persistentOrderInfo = em.find(OrderInfo.class, orderInfo.getOrderId());
			Carstore storeIdOld = persistentOrderInfo.getStoreId();
			Carstore storeIdNew = orderInfo.getStoreId();
			User userIdOld = persistentOrderInfo.getUserId();
			User userIdNew = orderInfo.getUserId();
			Car carIDOld = persistentOrderInfo.getCarID();
			Car carIDNew = orderInfo.getCarID();
			if (storeIdNew != null) {
				storeIdNew = em.getReference(storeIdNew.getClass(), storeIdNew.getStoreId());
				orderInfo.setStoreId(storeIdNew);
			}
			if (userIdNew != null) {
				userIdNew = em.getReference(userIdNew.getClass(), userIdNew.getUserId());
				orderInfo.setUserId(userIdNew);
			}
			if (carIDNew != null) {
				carIDNew = em.getReference(carIDNew.getClass(), carIDNew.getId());
				orderInfo.setCarID(carIDNew);
			}
			orderInfo = em.merge(orderInfo);
			if (storeIdOld != null && !storeIdOld.equals(storeIdNew)) {
				storeIdOld.getOrderInfoCollection().remove(orderInfo);
				storeIdOld = em.merge(storeIdOld);
			}
			if (storeIdNew != null && !storeIdNew.equals(storeIdOld)) {
				storeIdNew.getOrderInfoCollection().add(orderInfo);
				storeIdNew = em.merge(storeIdNew);
			}
			if (userIdOld != null && !userIdOld.equals(userIdNew)) {
				userIdOld.getOrderInfoCollection().remove(orderInfo);
				userIdOld = em.merge(userIdOld);
			}
			if (userIdNew != null && !userIdNew.equals(userIdOld)) {
				userIdNew.getOrderInfoCollection().add(orderInfo);
				userIdNew = em.merge(userIdNew);
			}
			if (carIDOld != null && !carIDOld.equals(carIDNew)) {
				carIDOld.getOrderInfoCollection().remove(orderInfo);
				carIDOld = em.merge(carIDOld);
			}
			if (carIDNew != null && !carIDNew.equals(carIDOld)) {
				carIDNew.getOrderInfoCollection().add(orderInfo);
				carIDNew = em.merge(carIDNew);
			}
			em.getTransaction().commit();
		} catch (Exception ex) {
			String msg = ex.getLocalizedMessage();
			if (msg == null || msg.length() == 0) {
				Integer id = orderInfo.getOrderId();
				if (findOrderInfo(id) == null) {
					throw new NonexistentEntityException("The orderInfo with id " + id + " no longer exists.");
				}
			}
			throw ex;
		} finally {
			if (em != null) {
				em.close();
			}
		}
	}

	public void destroy(Integer id) throws NonexistentEntityException {
		EntityManager em = null;
		try {
			em = getEntityManager();
			em.getTransaction().begin();
			OrderInfo orderInfo;
			try {
				orderInfo = em.getReference(OrderInfo.class, id);
				orderInfo.getOrderId();
			} catch (EntityNotFoundException enfe) {
				throw new NonexistentEntityException("The orderInfo with id " + id + " no longer exists.", enfe);
			}
			Carstore storeId = orderInfo.getStoreId();
			if (storeId != null) {
				storeId.getOrderInfoCollection().remove(orderInfo);
				storeId = em.merge(storeId);
			}
			User userId = orderInfo.getUserId();
			if (userId != null) {
				userId.getOrderInfoCollection().remove(orderInfo);
				userId = em.merge(userId);
			}
			Car carID = orderInfo.getCarID();
			if (carID != null) {
				carID.getOrderInfoCollection().remove(orderInfo);
				carID = em.merge(carID);
			}
			em.remove(orderInfo);
			em.getTransaction().commit();
		} finally {
			if (em != null) {
				em.close();
			}
		}
	}

	public List<OrderInfo> findOrderInfoEntities() {
		return findOrderInfoEntities(true, -1, -1);
	}

	public List<OrderInfo> findOrderInfoEntities(int maxResults, int firstResult) {
		return findOrderInfoEntities(false, maxResults, firstResult);
	}

	private List<OrderInfo> findOrderInfoEntities(boolean all, int maxResults, int firstResult) {
		EntityManager em = getEntityManager();
		try {
			CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
			cq.select(cq.from(OrderInfo.class));
			Query q = em.createQuery(cq);
			if (!all) {
				q.setMaxResults(maxResults);
				q.setFirstResult(firstResult);
			}
			return q.getResultList();
		} finally {
			em.close();
		}
	}

	public OrderInfo findOrderInfo(Integer id) {
		EntityManager em = getEntityManager();
		try {
			return em.find(OrderInfo.class, id);
		} finally {
			em.close();
		}
	}

	public int getOrderInfoCount() {
		EntityManager em = getEntityManager();
		try {
			CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
			Root<OrderInfo> rt = cq.from(OrderInfo.class);
			cq.select(em.getCriteriaBuilder().count(rt));
			Query q = em.createQuery(cq);
			return ((Long) q.getSingleResult()).intValue();
		} finally {
			em.close();
		}
	}
	
}
